/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.resources;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.internal.Policy;
import net.sourceforge.perforce.core.syncinfo.ResourceSyncInfo;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.PlatformObject;

/**
  * @version $Revision: 68 $
  * @author	<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public abstract class LocalResource extends PlatformObject
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the Perforce server. */
	private IPerforceServer server;
	/** Local Path of the resource. */
	private IPath location;
	
	// --------------------------------------------------------------- Public Static Factory Methods
	
	/** Creates either LocalFile or LocalFolder based on the given resource's type.
	  * 
	  * @param resource any resource.
	  * @return the corresponding LocalResource object.
	  */
	public static LocalResource createFromResource(IResource resource)
	{
		LocalResource localResource = null;
		try
		{
			IPerforceServer server = 
				PerforceCorePlugin.getServerManager().getPerforceServer(resource);	
			if (server != null)
			{
				if (resource instanceof IFile)
				{
					localResource = LocalFile.create(server, resource.getLocation());
				}
				else
				if (resource instanceof IContainer)
				{
					localResource = LocalFolder.create(server, resource.getLocation());
				}
			}
		}
		catch (PerforceException e)
		{
			PerforceCorePlugin.log(e.getStatus());
		}
		
		return localResource;
	}
	
	// -------------------------------------------------------------------------------- Constructors
	
	/** Constructor for LocalResource. 
	  * 
	  * @param server the perforce server.
	  * @param location the local location of the resource.
	  * 
	  * @pre server != null
	  * @pre location != null
	  */
	LocalResource(IPerforceServer server, IPath location)
	{
		Policy.assertNotNull(server);
		Policy.assertNotNull(location);
		
		this.server = server;
		this.location = location;
	}
	
	// ------------------------------------------------------------------------------ Public Methods
	
	/** Checks whether the local resource exists.
	  * 
	  * @return true if resource exists locally.
	  */
	public boolean exists()
	{
		return toResource().exists();
	}

	/** @return false */
	public boolean isIgnored()
	{	
		return false;
	}
	
	/** Returns the absolute pathname string of this abstract pathname. 
	  * 
	  * @return the absolute location.
	  */
	public String getAbsolutePath()
	{
		return getLocation().toOSString();
	}

	/** Returns the associated IResource.
	  * 
	  * @return the eclipse resource handle.
	  */
	public abstract IResource toResource();
	
	/** Returns the resource's sync info.
	  * 
	  * @return the sync info.
	  */
	public abstract ResourceSyncInfo getSyncInfo();

	/** Checks whether there is a remote object for this resource.
	  * 
	  * @return true if remote exists
	  */
	public abstract boolean hasRemote();
	
	/** Checks whether the resource is locally opened for edit. 
	  * 
	  * @return true if resource is opened for edit.
	  */
	public abstract boolean isCheckedOut();

	/** Checks whether the resource is dirty.
	  * 
	  * @return true if resource is dirty.
	  */
	public abstract boolean isDirty();

	/** Checks whether the local version is out of date.
	  * 
	  * @return true if local version is out of date.
	  */
	public abstract boolean isOutOfDate();

	/** Call this method to retrieve the perforce server.
	  * 
	  * @return the perforce server.
	  */
	public IPerforceServer getServer()
	{
		return server;
	}
	
	// ------------------------------------------------------------------------------------- Methods
	
	/** Call this method to retrieve the location.
	  * 
	  * @return the location.
	  */
	IPath getLocation()
	{
		return location;
	}
}
